package com.example.oms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.oms.common.enums.DeliveryCodeStatus;
import com.example.oms.common.enums.OmsOrderStatus;
import com.example.oms.common.result.R;
import com.example.oms.dto.entity.OmsOrderCommodityDetailEntity;
import com.example.oms.dto.entity.OmsOrderEntity;
import com.example.oms.dto.entity.OmsShopEntity;
import com.example.oms.dto.ro.ErpOrderCommodityDetailRO;
import com.example.oms.dto.ro.ErpOrderDetailRO;
import com.example.oms.mapper.OmsOrderCommodityDetailMapper;
import com.example.oms.mapper.OmsOrderLogMapper;
import com.example.oms.mapper.OmsOrderMapper;
import com.example.oms.mapper.OmsShopMapper;
import com.example.oms.service.ErpOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ErpOrderServiceImpl implements ErpOrderService {

    @Resource
    private OmsOrderMapper omsOrderMapper;

    @Resource
    private OmsOrderLogMapper omsOrderLogMapper;

    @Resource
    private OmsOrderCommodityDetailMapper omsOrderCommodityDetailMapper;

    @Resource
    private OmsShopMapper omsShopMapper;


    @Transactional
    @Override
    public R readOrderDetail(ErpOrderDetailRO erpOrderDetailRO) {
        //填充oms订单基本信息
        OmsOrderEntity omsOrderEntity = new OmsOrderEntity();
        fillOmsOrderEntity(erpOrderDetailRO, omsOrderEntity);
        //记录订单商铺信息
        String omsShopId = fillShopInformation(erpOrderDetailRO);
        omsOrderEntity.setOmsShopId(omsShopId);
        //记录商品信息
        fillOrderCommodityInformation(omsOrderEntity.getId(), erpOrderDetailRO);
        //插入订单
        int insert = omsOrderMapper.insert(omsOrderEntity);
        if (insert < 1) {
            return R.error(500,"插入订单信息失败");
        }

        return new R();
    }

    /**
     * 填充商品信息
     *
     * @param omsOrderId oms订单号
     * @param ro         erp推送的订单数据
     */
    private void fillOrderCommodityInformation(String omsOrderId, ErpOrderDetailRO ro) {
        List<OmsOrderCommodityDetailEntity> entities = new ArrayList<>();
        for (ErpOrderCommodityDetailRO commodityDetailRO : ro.getCommodities()) {
            OmsOrderCommodityDetailEntity commodityDetailEntity = new OmsOrderCommodityDetailEntity();
            BeanUtils.copyProperties(commodityDetailRO, commodityDetailEntity);
            commodityDetailEntity.setId(IdUtil.getSnowflake().nextIdStr());
            commodityDetailEntity.setOmsOrderId(omsOrderId);
            entities.add(commodityDetailEntity);
        }
        if (CollectionUtil.isNotEmpty(entities)) {
            omsOrderCommodityDetailMapper.insertBatch(entities);
        }
    }

    /**
     * 填充商铺信息
     *
     * @param ro erp推送的订单数据
     * @return oms记录的商铺id
     */
    private String fillShopInformation(ErpOrderDetailRO ro) {
        LambdaQueryWrapper<OmsShopEntity> eq = new QueryWrapper<OmsShopEntity>().lambda()
                .eq(OmsShopEntity::getErpShopId, ro.getErpShopId());
        OmsShopEntity omsShopEntity = omsShopMapper.selectOne(eq);
        OmsShopEntity shopEntity = new OmsShopEntity();
        if (ObjectUtils.isNotNull(omsShopEntity)) {
            String id = IdUtil.getSnowflake().nextIdStr();
            shopEntity.setId(id);
            shopEntity.setErpShopId(ro.getErpShopId());
            shopEntity.setShopName(ro.getShopName());
            omsShopMapper.insert(shopEntity);
            return id;
        } else {
            return shopEntity.getId();
        }
    }

    /**
     * 填充oms订单实体属性,设定订单为初始状态
     *
     * @param ro     erp推送的订单数据
     * @param entity oms订单实体
     */
    private void fillOmsOrderEntity(ErpOrderDetailRO ro, OmsOrderEntity entity) {
        BeanUtils.copyProperties(ro, entity);
        String id = IdUtil.getSnowflake().nextIdStr();
        entity.setId(id);
        entity.setStatus(OmsOrderStatus.TO_BE_CONFIRMED.getValue());
        entity.setDeliveryCodeStatus(DeliveryCodeStatus.UNSENT.getValue());
        entity.setCreateBy("");
        entity.setCreateTime(new Date());
        entity.setUpdateBy("");
        entity.setUpdateTime(new Date());
    }
}
